import api from './axios';

// 用户注册
export const register = async (userData) => {
  try {
    const response = await api.post('/users/register/', {
      username: userData.username,
      email: userData.email,
      password: userData.password,
      password_confirm: userData.password_confirm
    });
    return response.data;
  } catch (error) {
    throw error.response?.data || error;
  }
};

// 用户登录
export const login = async (credentials) => {
  try {
    console.log('demo444',credentials)
    const response = await api.post('/users/login/', {
      username: credentials.username,
      password: credentials.password
    });
    console.log('demo555',response)
    return response;
  } catch (error) {
    throw error.response?.data || error;
  }
};

// 获取用户信息
export const getUserInfo = async () => {
  try {
    const response = await api.get('/users/user/');
    return response.data;
  } catch (error) {
    throw error.response?.data || error;
  }
};

// 更新用户信息
export const updateUserProfile = async (profileData) => {
  try {
    const response = await api.put('/users/profile/', profileData);
    return response.data;
  } catch (error) {
    throw error.response?.data || error;
  }
}; 
export const logout = async () => {
    try {
      // 调用后端退出登录接口（如果有的话）
      await api.post('/users/logout/');
    } catch (error) {
      console.error('Logout API error:', error);
    } finally {
      // 无论后端请求是否成功，都清除本地存储的认证信息
      localStorage.removeItem('token');
    }
  };